package databaseLayer;

import java.util.List;

import model.MovieSection;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;

public class MovieSection_db {

    public static List<String> selectAllMovieNames() {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getThreadLocalSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("select distinct ms.movieName from MovieSection as ms");
            List<String> result = query.list();
            return result;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                HibernateUtil.closeSession();
            }
        }
        return null;
    }

    public static List<MovieSection> selectMovieSectionsByMovieName(String movieName) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getThreadLocalSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("from MovieSection where movieName=:movieName");
            query.setString("movieName", movieName);
            List<MovieSection> result = query.list();
            return result;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                HibernateUtil.closeSession();
            }
        }
        return null;
    }

    public static MovieSection selectMovieSectionById(String id) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getThreadLocalSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("from MovieSection where id=:id");
            query.setString("id", id);
            MovieSection result = (MovieSection) query.list().get(0);
            return result;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                HibernateUtil.closeSession();
            }
        }
        return null;
    }

    public static List<MovieSection> selectAllMovieSections() {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getThreadLocalSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("from MovieSection");
            List<MovieSection> result = query.list();
            return result;
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            if (session != null) {
                HibernateUtil.closeSession();
            }
        }
        return null;
    }
}
